package com.qire.manhua.controller;

import android.view.View;

import com.qire.antsrouter.AntsRouter;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.basic.StringUtil;
import com.qire.common.constant.Navigation;
import com.qire.common.model.entity.UserEntity;
import com.qire.common.support.CustomLoadDialog;
import com.qire.common.support.DialogFastBuildUtil;
import com.qire.common.support.DialogFastBuildUtil.Builder;
import com.qire.common.support.base.EventController;
import com.qire.common.support.base.ProxyViewModel;
import com.qire.common.utils.ToastHelper;
import com.qire.common.widget.snackbar.Prompt;
import com.qire.manhua.R;
import com.qire.manhua.activity.AccountsAndSecurityActivity;
import com.qire.manhua.dal.service.AccountsAndSecurityService;
import com.qire.manhua.databinding.ActivityAccountsAndSecurityBinding;
import com.qire.manhua.model.viewModel.IAccountsAndSecurityViewModel;
import com.qire.other.thirdPlatform.QqPlatform;
import com.qire.other.thirdPlatform.WeChatPlatform;

import androidx.annotation.NonNull;

public class AccountsAndSecurityController extends EventController<AccountsAndSecurityActivity> {

    private ProxyViewModel<IAccountsAndSecurityViewModel> viewModel;
    private AccountsAndSecurityService accountService;

    public AccountsAndSecurityController(@NonNull AccountsAndSecurityActivity lifecycleOwner) {
        super(lifecycleOwner);
    }

    @Override
    protected void bindView() {
        ActivityAccountsAndSecurityBinding viewDataBinder = owner.viewDataBinder();
        viewDataBinder.setAccountsAndSecurityController(this);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        this.viewModel = null;
        this.accountService = null;
    }

    public void onAddition(ProxyViewModel<IAccountsAndSecurityViewModel> viewModel) {
        this.viewModel = viewModel;
        this.accountService = new AccountsAndSecurityService(viewModel);
    }

    /**************************************************************************************************************/

    public void backBtnOnClick(View view) {
        owner.onBackPressed();
    }

    public void accountsAssociatedBtnOnClick(View view) {
        AntsRouter.ROUTER.buildCard(Navigation.Page.AccountsAssociatedPage).navigation();
    }

    public void mobileBindBtnOnClick(View view) {
        AntsRouter.ROUTER.buildCard(Navigation.Page.AccountBindGuidePage).navigation();
    }

    public void thirdPlatformBindBtnOnClick(View view) {
        viewModel.proxy().gotoTabPage(1);
    }

    public void changePasswordBtnOnClick(View view) {
        if(StringUtil.isEmpty(UserEntity.self.getMobile())) {
            ToastHelper.showSnackBar("请先绑定手机号", Prompt.WARNING);
            return ;
        }
        AntsRouter.ROUTER.buildCard(Navigation.Page.PasswordChangeGuidePage).navigation();
    }

    public void accountCancellationBtnOnClick(View view) {
        AntsRouter.ROUTER.buildCard(Navigation.Page.AccountCancellationGuidePage).navigation();
    }

    public void weChatBindBtnOnClick(View view) {
        String weChatName = UserEntity.self.getWeChatName();
        if(StringUtil.notEmpty(weChatName)) {
            // 如果绑定过
            final Builder builder = DialogFastBuildUtil.builder(R.layout.dialog_common_prompt_light_r);
            builder.setTextView(R.id.dialog_title, "解除绑定")
                    .setTextView(R.id.dialog_content, String.format("您的账号即将与微信(%s)\n进行解绑，是否需要再考虑一下呢？", weChatName))
                    .setTextView(R.id.dialog_button_dark, "取消", v -> {
                        builder.dismiss();
                        v.setOnClickListener(null);
                    })
                    .setTextView(R.id.dialog_button_light, "确认解绑", v -> {
                        builder.dismiss();
                        v.setOnClickListener(null);
                        operateWeChatTo(false);
                    })
                    .show();
        } else {
            // 如果未绑定
            operateWeChatTo(true);
        }
    }

    private void operateWeChatTo(boolean isBind) {
        CustomLoadDialog.show(owner, "请稍等", true, null);
        WeChatPlatform.WeChat.authorize((msg, info) -> {
            if(ObjectUtil.nonNull(info)) {
                if(isBind) {
                    accountService.weChatBind(info);
                } else {
                    accountService.weChatUnBind(info);
                }
            } else {
                ToastHelper.showToast(msg);
            }
            CustomLoadDialog.dismissDialog();
        });
    }

    public void qqBindBtnOnClick(View view) {
        String qqName = UserEntity.self.getQqName();
        if(StringUtil.notEmpty(qqName)) {
            // 如果绑定过
            final Builder builder = DialogFastBuildUtil.builder(R.layout.dialog_common_prompt_light_r);
            builder.setTextView(R.id.dialog_title, "解除绑定")
                    .setTextView(R.id.dialog_content, String.format("您的账号即将与QQ(%s)\n进行解绑，是否需要再考虑一下呢？", qqName))
                    .setTextView(R.id.dialog_button_dark, "取消", v -> {
                        builder.dismiss();
                        v.setOnClickListener(null);
                    })
                    .setTextView(R.id.dialog_button_light, "确认解绑", v -> {
                        builder.dismiss();
                        v.setOnClickListener(null);
                        operateQqTo(false);
                    })
                    .show();
        } else {
            // 如果未绑定
            operateQqTo(true);
        }
    }

    private void operateQqTo(boolean isBind) {
        CustomLoadDialog.show(owner, "请稍等", true, null);
        QqPlatform.build().authorize((msg, info) -> {
            if(ObjectUtil.nonNull(info)) {
                if(isBind) {
                    accountService.qqBind(info);
                } else {
                    accountService.qqUnBind(info);
                }
            } else {
                ToastHelper.showToast(msg);
            }
            CustomLoadDialog.dismissDialog();
        });
    }

}
